package com.zhuiyun.project.api.file.controller;

import com.zhuiyun.project.api.file.entity.FileManage;
import com.zhuiyun.project.api.file.service.FileManageService;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片服务器
 * @auther zcy
 * @data 2020/6/24 11:05
 */
@RestController
@RequestMapping("/image")
public class FileManageController extends BaseController {

    @Autowired
    FileManageService fileManageService;


    /**
     * 根据id获取图片
     * @param response
     * @param id
     */
    @GetMapping("/getImageById/{id}")
    public void getImage(HttpServletResponse response,@PathVariable("id") Integer id) throws CommonException {
        fileManageService.getImage(response,id);
    }
    /**
     * 根据id获取文件
     * @param response
     * @param id
     */
    @GetMapping("/getFileById")
    public void getFileById(HttpServletResponse response, Integer id) throws CommonException {
        fileManageService.getFileById(response,id);
    }
    /**
     * 根据id获取视频
     * @param response
     * @param id
     */
    @GetMapping("/getVideoById/{id}")
    public ResponseEntity<byte[]> getVideoById(HttpServletResponse response, @PathVariable("id")  Integer id) throws CommonException, IOException {
       return  fileManageService.getVideoById(response,id);
    }

    /**
     * 根据Url获取图片
     * @param response
     * @param url
     */
    @GetMapping("/getImageByUrl")
    public void getImageByUrl(HttpServletResponse response, String url) {
        fileManageService.getImageByUrl(response,url);

    }

    /**
     * 附件上传功能 将文件放在名为projectName的文件夹下
     * @param multipartRequest
     * @param projectName 项目名称
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFile/{projectName}")
    public CommonResult uploadFile(MultipartRequest multipartRequest, @PathVariable String projectName) throws Exception {
        MultipartFile file = multipartRequest.getFile("file");
        if(StringUtils.isNotEmpty(projectName)){
            FileManage fileManage = fileManageService.saveFileToSystem(file,projectName);
            if(fileManage.getId()>0){
                return CommonResult.ok(fileManage.getId());
            }
        }else{
            throw new CommonException(EmErrorCode.PROJECT_NAME_ISNULL);
        }
        return CommonResult.error();
    }
    /**
     * 附件上传功能 将图片放在other目录中
     * @param multipartRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFile")
    public CommonResult uploadFile(@RequestParam("files") MultipartFile file) throws Exception {

        String projectName = "other";
        FileManage fileManage = fileManageService.saveFileToSystem(file,projectName);
        if(fileManage.getId()>0){
            return CommonResult.ok(fileManage.getId());
        }
        return CommonResult.error();

    }

    /**
     * 删除图片及信息
     * @param id
     * @return
     * @throws CommonException
     */
    @DeleteMapping ("/deleteFileById")
    public CommonResult deleteImageById(Integer id) throws CommonException {
        return fileManageService.deleteImageById(id);
    }
}
